package com.xwj.controller.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xwj.data.StaticData;
import com.xwj.dto.JokeInfo;
import com.xwj.entity.RaRegisterUser;
import com.xwj.httpclient.HttpService;
import com.xwj.service.Impl.RegisterUserServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Created by thinkjoy on 2017/7/17.
 */
@RestController
@RequestMapping("/front/")
public class FrontIndexController {

    @Autowired
    private HttpService httpService;

    @RequestMapping(value = "index")
    public ModelAndView indexList(ModelAndView modelAndView, HttpServletRequest request) {
        String id = (String) request.getServletContext().getAttribute(StaticData.REGISTER_USER_LOGIN_ATTRIBUTE);
        if (StringUtils.isNotBlank(id)) {
            RaRegisterUser raRegisterUser = Optional.ofNullable(RegisterUserServiceImpl.getInstance().findUserById(id)).get();
            modelAndView.addObject("raRegisterUser", raRegisterUser);
        }
        modelAndView.setViewName("front/index");
        return modelAndView;
    }

    @RequestMapping(value = "myCenter")
    public ModelAndView myCenter(ModelAndView modelAndView, HttpServletRequest request) {
        String id = (String) request.getServletContext().getAttribute(StaticData.REGISTER_USER_LOGIN_ATTRIBUTE);
        if (StringUtils.isNotBlank(id)) {
            RaRegisterUser raRegisterUser = Optional.ofNullable(RegisterUserServiceImpl.getInstance().findUserById(id)).get();
            modelAndView.addObject("raRegisterUser", raRegisterUser);
        }
        modelAndView.setViewName("front/MyCenter");
        return modelAndView;
    }

    @RequestMapping(value = "myCenterContent")
    public ModelAndView myCenterContent(ModelAndView modelAndView) {
        modelAndView.addObject("JokeInfo", getJoke());
        modelAndView.setViewName("front/MyCenterContent");
        return modelAndView;
    }

    private JokeInfo getJoke() {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("page", String.valueOf(RandomUtils.nextInt(1, 50)));
            params.put("maxResult", StaticData.JOKE_MAX_RESULT);
            String result = httpService.doPost(StaticData.JOKE_API_URL, params);
            if (JSON.parseObject(result).getInteger("showapi_res_code") != 0)
                return null;
            JSONArray array = JSON.parseObject(result).getJSONObject("showapi_res_body").getJSONArray("contentlist");
            String title = array.getJSONObject(0).getString("title");
            String txt = array.getJSONObject(0).getString("text");
            return new JokeInfo(title, txt);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
